package com.guoshengdeu.door.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guoshengdeu.door.entity.Menu;
import com.guoshengdeu.door.service.IMenuService;
import com.guoshengdeu.door.util.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yfly
 * @since 2021-06-01
 */
@Api("菜单管理")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    //查询菜单
    @ApiOperation("查询")
    @RequestMapping("list")
    public List<Menu> queryMenu(){
        return menuService.list();
    }

    //二级菜单查询
    @ApiOperation("二级菜单")
    @GetMapping("smenu")
    public  List<Map<String,Object>> findBy(){
        List<Map<String, Object>> leadData = menuService.findById();
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < leadData.size(); i++){
            Map<String, Object> one = new HashMap<>();
            one.put("lname",leadData.get(i).get("lname"));
            one.put("lurl",leadData.get(i).get("lurl"));
            List<Map<String, Object>> thisLead = menuService.findByIdList(Integer.parseInt(leadData.get(i).get("lid").toString()));
            List<Map<String, Object>> date = new ArrayList<>();
            for (int j = 0; j < thisLead.size(); j++){
                Map<String, Object> mp = new HashMap<String, Object>();
                mp.put("menuName",thisLead.get(j).get("lname"));
                mp.put("lurl",thisLead.get(j).get("lurl"));
                date.add(mp);
            }
            one.put("dataList",date);
            dataList.add(one);
        }
        return dataList;
    }

    //添加更新
    @ApiOperation("添加更新")
    @RequestMapping("addmenu")
    public RespResult addMenu(@RequestBody Menu menu){
        boolean saveOrUpdate = menuService.saveOrUpdate(menu);
        if (saveOrUpdate){
            return RespResult.success();
        }
        return RespResult.error("添加或更新失败");
    }

    //删除
    @ApiOperation("删除")
    @DeleteMapping("delemenu")
    public RespResult deleMenu(int id){
        boolean removeById = menuService.removeById(id);
        if (removeById){
            return RespResult.success();
        }
        return RespResult.error("删除失败");
    }

    //分页
    @ApiOperation("分页")
    @GetMapping("page")
    public RespResult page(Page<Menu> page){
        Page<Menu> pages = menuService.page(page);
        return RespResult.success(pages);
    }

}
